﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cadbom
{
    public partial class HongShengFeiForm : Form
    {
        HongShengFeiProcess process = null;
        NetApi netApi = new NetApi();

        public HongShengFeiForm()
        {
            netApi.InitDomainConfig();
            netApi.getUserInfo();
            process = new HongShengFeiProcess();
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            process.UnlockAllLayersAndReadObject(new Action<List<Dictionary<String, Dictionary<String, String>>>>(r =>
            {
                netApi.saveProcessBom(r, new Action<string>(result =>
                {

                    int i = 0;
                    r.ForEach(p =>
                    {
                        i++;
                        this.richTextBox1.AppendText("第" + i + "图" + " \n");
                        foreach (var key in p.Keys)
                        {
                            //BomForm bomForm = new BomForm();
                            //bomForm.TT7SaveBomFormData(key, result);


                            this.richTextBox1.AppendText("产品 : " + key + " \n");
                            foreach (var key1 in p[key].Keys)
                            {
                                this.richTextBox1.AppendText(key1 + ":" + p[key][key1] + " \n");
                            }
                        }


                    });
                }));

            }));
            //process.UnlockAllLayersAndReadObject(new Action<List<Dictionary<String, Dictionary<String, String>>>>(r =>
            //{
            //    int i = 0;
            //    r.ForEach(p =>
            //    {
            //        i++;
            //        this.richTextBox1.AppendText("第" + i + "图" + " \n");
            //        foreach (var key in p.Keys)
            //        {

            //            this.richTextBox1.AppendText(key + ":" + p[key] + " \n");
            //        }
                   
            //    });
            //}));
            //process.ExecuteLisp(new Action<string>(r =>
            //{
            //    // 获取当前时间并格式化
            //    string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //    this.richTextBox1.AppendText(timestamp + " 坐标点读取完成!\n" );
            //    process.UnlockAllLayersAndReadObject();
            //}));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            process.ExecuteLisp(new Action<String> (r =>{
                // 假设这是你的Form类的一个方法
                this.Invoke(new Action(() =>
                {
                    // 更新UI的代码
                    this.richTextBox1.AppendText(" 坐标点读取完成!\n");
                }));
                
            }));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            process.readProductMessages(new Action<List<Dictionary<String, Dictionary<String, String>>>>(r =>
            {

                List<BasProductImport> importProductList = new List<BasProductImport>();
                int i = 0;
                String mainProductName = "";
                r.ForEach(p =>
                {

                    foreach (var key in p.Keys)
                    {
                        i++;
                        //BomForm bomForm = new BomForm();
                        //bomForm.TT7SaveBomFormData(key, result);
                        BasProductImport importProduct = new BasProductImport();

                        importProduct.name = key;

                        this.richTextBox1.AppendText("产品 : " + key + " \n");
                        foreach (var key1 in p[key].Keys)
                        {
                            this.richTextBox1.AppendText(key1 + ":" + p[key][key1] + " \n");
                            if (key1 == "材料")
                            {
                                importProduct.description = p[key][key1];

                            }
                            if (key1 == "handleid")
                            {
                                importProduct.cadHandleId = p[key][key1];
                            }
                            if (key1 == "产品名称")
                            {
                                mainProductName = p[key][key1];
                            }
                            if (key1 == "数量")
                            {
                                importProduct.importQty = p[key][key1];
                            }
                        }
                        importProduct.customerCode = "C001";
                        importProduct.unitName = "PCS";
                        DateTime now = DateTime.Now;
                        Random random = new Random();
                        importProduct.custCode = "2025416" + i;
                        importProduct.productVer = "A";
                        importProduct.productTypeName = "半成品";
                        importProduct.currencyName = "人民币";
                        importProductList.Add(importProduct);
                    }

                });
                if (importProductList.Count() > 0)
                {
                    if (mainProductName != null)
                    {
                        BasProductImport importProduct = new BasProductImport();
                        importProduct.name = mainProductName;
                        importProduct.customerCode = "C001";
                        importProduct.unitName = "PCS";
                        DateTime now = DateTime.Now;
                        Random random = new Random();
                        importProduct.custCode = "2025416" + 999;
                        importProduct.productVer = "A";
                        importProduct.productTypeName = "成品";
                        importProduct.currencyName = "人民币";
                        importProductList.Add(importProduct);
                    }
                    netApi.saveProductList(importProductList, new Action<List<BasProduct>>(result =>
                    {
                        Object aa = result;
                        process.updateCadProductCode(result, new Action<string>(rr =>
                        {
                            string aa1 = rr;
                        }));
                    }));
                }

                //netApi.saveProcessBom(r, new Action<string>(result =>
                //{

                //    int i = 0;
                //    r.ForEach(p =>
                //    {
                //        i++;
                //        this.richTextBox1.AppendText("第" + i + "图" + " \n");
                //        foreach (var key in p.Keys)
                //        {
                //            //BomForm bomForm = new BomForm();
                //            //bomForm.TT7SaveBomFormData(key, result);


                //            this.richTextBox1.AppendText("产品 : " + key + " \n");
                //            foreach (var key1 in p[key].Keys)
                //            {
                //                this.richTextBox1.AppendText(key1 + ":" + p[key][key1] + " \n");
                //            }
                //        }


                //    });
                //}));

            }));
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}
